﻿using FanaticRock.Theme.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace FanaticRock.Theme.Custom
{
    ///<summary>卡片通知的主对象。</summary>
    public class NoticeCardHost : Canvas
    {
        #region dp props

        public Style NoticeCardStyle
        {
            get { return (Style)GetValue(NoticeCardStyleProperty); }
            set { SetValue(NoticeCardStyleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NoticeCardStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NoticeCardStyleProperty =
            DependencyProperty.Register("NoticeCardStyle", typeof(Style), typeof(NoticeCardHost), new PropertyMetadata(null));


        public double StartOffset { get { return (double)GetValue(StartOffsetProperty); } set { SetValue(StartOffsetProperty, value); } }

        // Using a DependencyProperty as the backing store for StartOffset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StartOffsetProperty = DependencyProperty.Register("StartOffset", typeof(double), typeof(NoticeCardHost), new PropertyMetadata(-500d));

        #endregion



        #region methods


        public async void Notice(NoticeEnum type, string title, string content)
        {
            NoticeCard noticeCard = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                noticeCard = new NoticeCard();

                noticeCard.Style = NoticeCardStyle;
                Canvas.SetTop(noticeCard, 0d);
                Canvas.SetLeft(noticeCard, StartOffset);

                noticeCard.NoticeType = type;
                noticeCard.Title = title;
                noticeCard.Content = content;

                Children.Add(noticeCard);

                var anim = new DoubleAnimation()
                {
                    From = StartOffset,
                    To = 0,
                    EasingFunction = new QuinticEase() { EasingMode = EasingMode.EaseOut },
                    Duration = new Duration(TimeSpan.FromMilliseconds(300))
                };

                noticeCard.BeginAnimation(Canvas.LeftProperty, anim);

                Resort();
            });


            await Task.Delay(1300);

            if (noticeCard == null) return;

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                Children.Remove(noticeCard);
            }));

        }

        private void Resort()
        {
            var counter = 1;
            var height = 135;
            foreach (var item in Children.OfType<NoticeCard>().Reverse().Skip(1))
            {
                var targetY = -counter * height;
                var anim = new DoubleAnimation(targetY, new Duration(TimeSpan.FromMilliseconds(150)))
                {
                    EasingFunction = new QuinticEase() { EasingMode = EasingMode.EaseOut },
                };
                item.BeginAnimation(Canvas.TopProperty, anim);
                counter++;
            }
        }


        #endregion




    }
}
